<?php

/*
 *  @autor: peter dobsa
 */
 
class Lectures_model extends CI_Model
{

	//vrati pole vsetkych prednasok
	function GetLectures()
	{
		$q = $this->db->get('lectures');
		return $q->result();
	}
	
	//vrati prednasku s danym id-ckom, alebo null ak neexistuje
	function GetLectureById($id)
	{
		$q = $this->db->get_where('lectures', array('id' => $id));
		return $q->num_rows() == 0 ? null : $q->first_row();
	}
	
	//vrati prednasku s danym menom, alebo null ak neexistuje
	function GetLectureByName($name)
	{
		$q = $this->db->get_where('lectures', array('lecture_name' => $name));
		return $q->num_rows() == 0 ? null : $q->first_row();
	}
	
	//prida novu prednasku
	//vstup: asociativne pole, kde klucom je meno stlpca v databaze
	//vrati uspesnost operacie ako boolean hodnotu
	function InsertLecture($lecture)
	{
		if (!array_key_exists('lecture_name', $user))  
		{
			return false;
		}
		if ($this->GetLectureByName($user['lecture_name']) != null) return false;
		return $this->db->insert('lectures', $user);
	}
	
	//aktualizuje prednasku s danym id-ckom
	//dostane ako vstup id prednasky a nove udaje ako asociativne pole pri inserte
	//vrati uspesnost operacie
	function UpdateLectureById($id, $lecture)
	{
		$this->db->where('id', $id);
		return $this->db->update('lectures', $lecture);
	}
	
	//aktualizuje prednasku s danym id-ckom
	//dostane ako vstup id prednasky a nove udaje ako asociativne pole pri inserte
	//vrati uspesnost operacie
	function UpdateLectureByName($name, $lecture)
	{
		$this->db->where('lecture_name', $name);
		return $this->db->update('lectures', $lecture);
	}
	
	//odstrani predsnaku s id-ckom $id
	function RemoveLecture($id)
	{
		$this->db->delete('lecture', array('id' => $id));
	}
	
	//vrati pole vsetkych zadani
	function GetAssignments()
	{
		$q = $this->db->get('assignments');
		return $q->result();
	}
	
	function GetAssignmentsByLecture($lecture)
	{
		//TODO najprv db
	}
	
	function GetAssignmentsByCourse($couseId)
	{
		//TODO najorv db
	}
	
	//vrati zadanie podla mena
	function GetAssignmentByName($name)
	{
		$q = $this->db->get_where('assignments', array('assignment_name' => $name));
		return $q->num_rows() == 0 ? null : $q->first_row();
	}
	
	//vrati zadanie podla id-cka
	function GetAssignmentById($id)
	{
		$q = $this->db->get_where('assignments', array('id' => $id));
		return $q->num_rows() == 0 ? null : $q->first_row();
	}
	
	//TODO ulozenie suborov do db

}